package binarySearch;

public class B1IidL069 {
    public int peakIndexInMountainArray(int[] arr) {
        return binarySearch(arr);
    }

    public int binarySearch(int[] arr) {
        int begin = 0;
        int end = arr.length - 1;
        while (begin <= end) {

            int mid = (end - begin) / 2 + begin;

            if (mid == begin || mid == end) {
                return arr[begin]>arr[end]?begin:end;
            }

            if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) {
                return mid;
            }

            if(arr[mid]<=arr[mid-1]){
                end = mid-1;
            }
            if(arr[mid]<=arr[mid+1]){
                begin = mid+1;
            }

        }
        return 0;

    }


}
